package com.wiretrack.circuit;

import java.io.File;

import org.apache.log4j.Logger;

public final class Utils
{
	private static Logger log = Logger.getLogger(Utils.class.getName());

	public static void speak(Configuration config, String textToSpeak, int delayInSeconds)
	{
		if ( textToSpeak != null && textToSpeak.length() > 0 )
		{
			if ( config.speechcommand != null && config.speechcommand.length() > 0 )
			{
				String commandToExecute = config.speechcommand.replaceAll("\\$text", "\"" + textToSpeak + "\"");
				log.debug("Speaking sound: " + commandToExecute);
				TextToSpeechPlayer player = new TextToSpeechPlayer(commandToExecute, delayInSeconds);
				player.start();
			}
		}
		else
		{
			log.debug("No text defined to speak");
		}
	}

	public static void playSound(Configuration config, String sound)
	{
		if ( sound != null && sound.length() > 0 )
		{
			File potentialSoundToPlay = new File(sound);
			
			if ( potentialSoundToPlay.exists() )
			{
				String commandToExecute = config.soundcommand.replaceAll("\\$sound", sound);
				SoundPlayer player = new SoundPlayer(commandToExecute);
				player.start();
			}
			else
			{
				log.warn("Sound file does not exist so not playing: " + sound);
			}
		}
		else
		{
			log.debug("No sound defined so not playing anything");
		}
	}
}
